<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<!-- 1、有如下结构的树结构，定义一个函数findName，要求输入id，则返回对应的name
+ findName(1, tree)，返回“浙江”
+ findName(20, tree)，返回“济南”
+ findName(212, tree), 返回“烟台2”

const tree = [
  {
      id: 1,
    name: '浙江',
    children: [
      {
          id: 10,
        name: '杭州',
        children: [
          {
              id: 100,
        name: '西湖',
          },
          {
              id: 101,
        name: '余杭',
          },
        ],
      },
      {
          id: 11,
        name: '绍兴',
        children: [
          {
              id: 111,
        name: '绍兴1',
          },
          {
              id: 112,
        name: '绍兴2',
          },
        ],
      }
    ],
  },
  {
      id: 2,
    name: '山东',
    children: [
      {
          id: 20,
        name: '济南',
        children: [
          {
              id: 200,
        name: '济南1',
          },
          {
              id: 201,
        name: '济南2',
          },
        ],
      },
      {
          id: 21,
        name: '烟台',
        children: [
          {
              id: 211,
        name: '烟台1',
          },
          {
              id: 212,
        name: '烟台2',
          },
        ],
      }
    ],
  },
  {
      id: 3,
    name: '澳门',
    children: [
      {
          id: 30,
        name: '澳门1'
      },
      {
          id: 31,
        name: '澳门2'
      }
    ],
  }
]
``` -->

<body>
    <div>test15-1</div>
    <script>
        const tree = [
            {
                id: 1,
                name: '浙江',
                children: [
                    {
                        id: 10,
                        name: '杭州',
                        children: [
                            {
                                id: 100,
                                name: '西湖',
                            },
                            {
                                id: 101,
                                name: '余杭',
                            },
                        ],
                    },
                    {
                        id: 11,
                        name: '绍兴',
                        children: [
                            {
                                id: 111,
                                name: '绍兴1',
                            },
                            {
                                id: 112,
                                name: '绍兴2',
                            },
                        ],
                    }
                ],
            },
            {
                id: 2,
                name: '山东',
                children: [
                    {
                        id: 20,
                        name: '济南',
                        children: [
                            {
                                id: 200,
                                name: '济南1',
                            },
                            {
                                id: 201,
                                name: '济南2',
                            },
                        ],
                    },
                    {
                        id: 21,
                        name: '烟台',
                        children: [
                            {
                                id: 211,
                                name: '烟台1',
                            },
                            {
                                id: 212,
                                name: '烟台2',
                            },
                        ],
                    }
                ],
            },
            {
                id: 3,
                name: '澳门',
                children: [
                    {
                        id: 30,
                        name: '澳门1'
                    },
                    {
                        id: 31,
                        name: '澳门2'
                    }
                ],
            }
        ]
        let a
        function findName(id, tree) {
            for (let i = 0; i < tree.length; i++) {
                if (tree[i].id == id) {
                    a = tree[i].name
                }
                if (tree[i].children) {
                    findName(id, tree[i].children)
                }
            }
            return a
        }
     
        console.log(findName(1, tree)) //返回“浙江”
        console.log(findName(20, tree)) //返回“济南”
        console.log(findName(212, tree))  ///返回“烟台2”
    </script>
</body>

</html>